<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
//加密函数
function lock_url($txt,$key='zhuoyuexiazai'){
	$txt = $txt.$key;
	$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
	$nh = rand(0,64);
	$ch = $chars[$nh];
	$mdKey = md5($key.$ch);
	$mdKey = substr($mdKey,$nh%8, $nh%8+7);
	$txt = base64_encode($txt);
	$tmp = '';
	$i=0;$j=0;$k = 0;
	for ($i=0; $i<strlen($txt); $i++) {
		$k = $k == strlen($mdKey) ? 0 : $k;
		$j = ($nh+strpos($chars,$txt[$i])+ord($mdKey[$k++]))%64;
		$tmp .= $chars[$j];
	}
	return urlencode(base64_encode($ch.$tmp));
}
//解密函数
function unlock_url($txt,$key='zhuoyuexiazai'){
	$txt = base64_decode(urldecode($txt));
	$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
	$ch = $txt[0];
	$nh = strpos($chars,$ch);
	$mdKey = md5($key.$ch);
	$mdKey = substr($mdKey,$nh%8, $nh%8+7);
	$txt = substr($txt,1);
	$tmp = '';
	$i=0;$j=0; $k = 0;
	for ($i=0; $i<strlen($txt); $i++) {
		$k = $k == strlen($mdKey) ? 0 : $k;
		$j = strpos($chars,$txt[$i])-$nh - ord($mdKey[$k++]);
		while ($j<0) $j+=64;
		$tmp .= $chars[$j];
	}
	return trim(base64_decode($tmp),$key);
} 
function p($a){
	echo '<pre>';
	print_r($a);die;
	
}
//二维数组
function  two_dimensional_array($list,$key){
	$ref=[];
	foreach ($list as $rkey=>$val){
		if($val[$key]==0){
			$ref[]=$val;
		}
	}
	
	foreach ($ref as $rkey1=>$val1){
		foreach ($list as $rkey2=>$val2){
			if($val1['id']==$val2[$key]){
				$ref[$rkey1]['children'][]=$val2;
			}
		}
	}
	return $ref;
}
function juli_str($lat,$lng){
	$juli=" ROUND(
			        6378.138 * 2 * ASIN(
			            SQRT(
			                POW(
			                    SIN(
			                        (
			                            $lat * PI() / 180 - lat * PI() / 180
			                        ) / 2
			                    ),
			                    2
			                ) + COS($lat * PI() / 180) * COS(lat * PI() / 180) * POW(
			                    SIN(
			                        (
			                            $lng * PI() / 180 - lng * PI() / 180
			                        ) / 2
			                    ),
			                    2
			                )
			            )
			        ) * 1000
			    ) AS juli
			";
    return $juli;
}
/**
 * 获取网路图片的长宽
 * @param unknown $url
 * @return string[]|unknown[]
 */
function getImageinfo($url)
{
	$result = array(
			'width'=>'',
			'height'=>'',
			'size'=>'',
	);
	$imageInfo = getimagesize($url);
	$result['width']=$imageInfo[0];
	$result['height']=$imageInfo[1];
	
	$headerInfo = get_headers($url,true);
	$result['size']=$headerInfo['Content-Length'];
	
	return $result;
}

